html="""
    <html><head><title>The Dormouse's story</title></head>
    <body>
    <p class="title" name="dromouse"><b>The Dormouse's story</b></p>
    <p class="story">Once upon a time there were three little sisters; and their names were
    <a href="http://example.com/elsie" class="sister" id="link1"><!-- Elsie --></a>,
    <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
    <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>; 
    and they lived at the bottom of a well.</p>
    <p class="story">...</p>
    """
from bs4 import BeautifulSoup
soup=BeautifulSoup(html,'lxml')
# print(soup.prettify())
'''
选择节点，提取内容
'''
print(soup.title)
print(type(soup.title))
print(soup.title.string)
print(soup.head)
print(soup.head.title.string)

print(soup.title.name)

#多个节点只获取第一个
print(soup.p)
print(soup.p.string)
print(soup.p.attrs)
print(soup.p.attrs['name'])

'''
嵌套选择
'''
print(soup.head.title)
print(type(soup.head.title))
print(soup.head.title.string)




html="""
    <html><head><title>The Dormouse's story</title></head>
    <body>
    <p class="story">Once upon a time there were three little sisters; and their names were
    <a href="http://example.com/elsie" class="sister" id="link1">
    <span><!-- Elsie --></span>
    </a>,

    <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
    <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>; 
    and they lived at the bottom of a well.</p>

    <p class="story">...</p>
    """

'''
关联选择
'''
soup=BeautifulSoup(html,'lxml')
print(soup.p.contents)

print(soup.p.children)
for i,child in enumerate(soup.p.children):
    print(i,child)


print(soup.p.descendants)
for i,child in enumerate(soup.p.descendants):
    print(i,child)


'''
父节点
'''
print(soup.p.parent)
print(soup.p.parents)
print(list(enumerate(soup.p.parents)))

'''
兄弟节点
'''
print('Next Sibling',soup.a.next_sibling)
print('Pre Sibling',soup.a.previous_sibling)
print('Next Siblings',list(enumerate(soup.a.next_siblings)))
print('Pre Siblings',list(enumerate(soup.a.previous_siblings)))

'''
提取信息
'''
print(list(soup.a.parents)[0].attrs['class'])


'''
方法选择器
'''
html='''
    <div class="panel">
    <div class="panel-heading">
    <h4>Hello</h4>
    </div>
    <div class="panel-body">
    <ul class="list" id="list-1" name="elements">
    <li class="element">Foo</li>
    <li class="element">Bar</li>
    <li class="element">Jay</li>
    </ul>

    <ul class="list list-small" id="list-2">
    <li class="element">Foo</li>
    <li class="element">Bar</li>
    </ul>
    </div>
    </div>
'''
'''
name
'''
soup=BeautifulSoup(html,'lxml')
print(soup.find_all(name='ul'))
print(type(soup.find_all(name='ul')[0]))

for ul in soup.find_all(name='ul'):
    print(ul.find_all(name='li'))
    for li in ul.find_all(name='li'):
        print(li.string)

'''
attrs
'''
print(soup.find_all(attrs={'id':'list-1'}))
print(soup.find_all(attrs={'name':'elements'}))

print(soup.find_all(id='list-1'))
print(soup.find_all(class_='element'))

'''
text
import re
print(soup.find_all(text-re.compile('link')))
'''


'''
css 选择器
'''
html='''
    <div class="panel">
    <div class="panel-heading">
    <h4>Hello</h4>
    </div>
    <div class="panel-body">
    <ul class="list" id="list-1">
    <li class="element">Foo</li>
    <li class="element">Bar</li>
    <li class="element">Jay</li>
    </ul>

    <ul class="list list-small" id="list-2">
    <li class="element">Foo</li>
    <li class="element">Bar</li>
    </ul>
    </div>
    </div>
    '''

soup=BeautifulSoup(html,'lxml')
print(soup.select('.panel .panel-heading'))
print(soup.select('ul li'))
print(soup.select('#list-2 .element'))
print(type(soup.select('ul')[0]))

'''
嵌套选择
'''
for ul in soup.select('ul'):    
    print(ul.select('li'))

'''
获取属性
'''
for ul in soup.select('ul'):    
    print(ul['id'])
    print(ul.attrs['id'])

'''
获取文本
'''
for li in soup.select('li'):    
    print('Get Text',li.get_text())
    print('String',li.string)
































